package study.ttl.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import study.ttl.common.Result;
import study.ttl.entity.Carousel;
import study.ttl.service.CarouselService;

import java.util.List;

@RestController
@RequestMapping("/carousel")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    /**
     * 添加
     * @param carousel
     * @return
     */
    @SaCheckRole("ADMIN")
    @PostMapping("/add")
    public Result register(@RequestBody Carousel carousel) {
        carouselService.add(carousel);
        return Result.success();
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @SaCheckRole("ADMIN")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        carouselService.deleteById(id);
        return Result.success("删除成功");
    }

    /**
     * 根据id批量删除
     * @param ids
     * @return
     */
    @SaCheckRole("ADMIN")
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        carouselService.deleteBatch(ids);
        return Result.success("删除成功");
    }

    /**
     * 修改信息
     * @param carousel
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Carousel carousel) {
        carouselService.update(carousel);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Carousel carousel = carouselService.selectById(id);
        return Result.success(carousel);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(Carousel carousel) {
        List<Carousel> carouselList = carouselService.selectAll(carousel);
        return Result.success(carouselList);
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param carousel
     * @return
     */
    @SaCheckRole("ADMIN")
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Carousel carousel) {
        PageInfo<Carousel> carouselList = carouselService.selectPage(pageNum, pageSize, carousel);
        return Result.success(carouselList);
    }
}
